package com.mes.cloud.base;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Data
public class ServiceInfo {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${spring.application.version}")
    private String version;

    @Value("${server.port:${SERVER_PORT:${PORT:8080}}}")
    private int port;

    private String ipAddress;

    @Autowired
    InetUtils inetUtils;

    @PostConstruct
    public void init() {
        ipAddress = inetUtils.findFirstNonLoopbackAddress().getHostAddress();
    }

    @Override
    public String toString() {
        return String.format("%s(%s:%s)", applicationName, ipAddress, port);
    }


}